package com.heima.search.test;

import com.heima.feignclient.client.ArticleFeign;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.search.service.MySearchService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.CollectionUtils;

import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class Db2Es {

    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private MySearchService mySearchService;

    @Test
    public void  db2Es(){

        int page = 1, size = 3;
        while (true) {
            System.out.println("page==="+page);
//        远程调用article服务，分页查询article文章数据
            List<ArticleDto> articleDtoList = articleFeign.findByPage(page, size);
            if(CollectionUtils.isEmpty(articleDtoList)){
//                没有返回数据
                break;
            }
//        把数据库数据导入Es
            mySearchService.createIndex(articleDtoList);
            if(articleDtoList.size()<size){
//                最后一页
                break;
            }
            page++;
        }
    }
}
